require 'View/UIBase/UIBase'

UIFavorUp = UIBase.New(PanelNames.UIFavorUp);
local this=UIFavorUp
function this.Init(favorUpInfos)
    this.favorUpInfos=favorUpInfos
    UIBase.Init(this)
end

function this:OnCreate()
    self.btnClose=self:GetChild("BgClose")
    self.itemHero=self:GetChild("hero")
    self:addEvent();
    self.headCtrl= CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    self.txtLastLvl=self:GetChild("level/num")
    self.txtNextLvl=self:GetChild("level/upNum")
    self.txtDesc=self:GetChild("text")
    self.ctrlType=self:GetController("type")
    self.listHero=self:GetChild("heroList")
end

function this:OnEnable()
    if table.isNullOrEmpty(self.favorUpInfos)then
        self:OnClickClose()
        return
    end
    logicMgr.GuideMgr.Pause(GuideMgr.PauseType.favorPause)
    this:Refresh()
end

function this:OnDisable()
    logicMgr.GuideMgr.Resume(GuideMgr.PauseType.favorPause)
end

function this:OnDestroy()

end
-------------------------------function--------------------------------------
function this:OnClickClose()
    self:OnBack()
end

function this:addEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
end

function this:Refresh()
    if #self.favorUpInfos==1 then
        self:SetSingle()
    else
        self:SetMultiple()
    end
end
function this:SetSingle()
    self.ctrlType.selectedIndex=0
    local data=self.favorUpInfos[1]
    self.txtLastLvl.text=tostring(data.lastLevel)
    self.txtNextLvl.text=tostring(data.level)
    local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(data.partnerId,true)
    self.headCtrl.initData(self.itemHero,heroData)
    self:SetHead(self.itemHero)
    local rewardData=logicMgr.HeroFavorManager.getRewardData(heroData.partnerModelId)[data.level]
    if rewardData.f_RewardType~=3 then
        local language=getLanguage("HeroFavorTips"..rewardData.f_RewardType)
        self.txtDesc.text=string.format(language,tostring(rewardData.f_RewardCo))
    else
        self.txtDesc.text=""
    end
end
function this:SetMultiple()
    self.ctrlType.selectedIndex=1
    self.listHero:RemoveChildrenToPool()
    for i,v in ipairs(self.favorUpInfos)do
        local item=self.listHero:AddItemFromPool()
        local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(v.partnerId,true)
        local head=item:GetChild("hero")
        self.headCtrl.initData(head,heroData)
        self:SetHead(head)
        local txtNum=item:GetChild("num")
        local txtNextNum=item:GetChild("upNum")
        txtNum.text=tostring(v.lastLevel)
        txtNextNum.text=tostring(v.level)
    end
end
function this:SetHead(head)
    local ctrlLvl=head:GetController("isShowLevel")
    local ctrlHadBattle=head:GetController("ctrlHadBattle")
    ctrlLvl.selectedIndex=1
    ctrlHadBattle.selectedIndex=0
end
return this